package com.xj.qq.adapters;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xj.qq.ChatRoom;
import com.xj.qq.MainActivity;
import com.xj.qq.R;
import com.xj.qq.domain.Conversation;
import com.xj.qq.swipe.SwipeItemLayout;

import org.w3c.dom.Text;

import java.util.ArrayList;

/**
 * Created by XJ on 2015/12/24
 */
public class ConversationAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Conversation> als;
    private final int TYPE_1 = 0;
    private final int TYPE_2 = 1;

    public ConversationAdapter(Context context, ArrayList<Conversation> als) {

        this.context = context;
        this.als = als;
    }

    @Override
    public int getCount() {
        return als.size();
    }

    @Override
    public Object getItem(int position) {
        return als.get(position);
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0){
            return TYPE_1;
        }else {
            return TYPE_2;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {

        int type = getItemViewType(position);

        ViewHolder vh = null;

        if (convertView == null){
            switch (type){
                case TYPE_1:
                    final Button btn_chat = new Button(context);
                    final EditText ev = new EditText(context);
                    LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

                    ev.setLayoutParams(ll);
                    ev.setGravity(Gravity.CENTER);
                    ev.setText("");
                    ev.setTextSize(12);
                    ev.setTextColor(context.getResources().getColor(R.color.colorGray_2));
                    ev.setBackground(context.getDrawable(R.drawable.cvs_search_shape));
                    ev.setPadding(0, 15, 0, 15);

                    btn_chat.setLayoutParams(ll);
                    btn_chat.setGravity(Gravity.CENTER);
                    btn_chat.setText("☆搜索☆");
                    btn_chat.setTextSize(20);
                    btn_chat.setTextColor(context.getResources().getColor(R.color.blue));
                    btn_chat.setPadding(0, 0, 0, 0);
                    if(!ev.equals(null)) {
                        btn_chat.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent();
                                intent.putExtra("address", ev.getText().toString());
                                intent.setClass(context, ChatRoom.class);
                                parent.getContext().startActivity(intent);

                             }
                         });
                    }
                    RelativeLayout l = new RelativeLayout(context);
                    ll.setMargins(30, 0, 30, 0);
                    l.setLayoutParams(ll);
                    l.addView(ev);
                    ll.setMargins(20, 70, 20, 0);
                    l.setLayoutParams(ll);
                    l.addView(btn_chat);
                    convertView = l;

                    break;

                case TYPE_2:

                    View view1 = LayoutInflater.from(context).inflate(R.layout.ls_conversation_layout,
                            parent,false);
                    vh = new ViewHolder();
                    vh.cvs_icon = (ImageView) view1.findViewById(R.id.iv_cvs_icon);
                    vh.cvs_name = (TextView) view1.findViewById(R.id.tv_cvs_name);
                    vh.cvs_content = (TextView) view1.findViewById(R.id.tv_cvs_content);
                    vh.cvs_date = (TextView) view1.findViewById(R.id.tv_cvs_date);

                    View view2 = LayoutInflater.from(context).inflate(R.layout.swipe_layout,
                            parent,false);
                    vh.cvs_top = (TextView) view2.findViewById(R.id.to_top);
                    vh.cvs_delete = (TextView) view2.findViewById(R.id.delete_item);
                    vh.cvs_top.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                        }
                    });
                    vh.cvs_delete.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            als.remove(position-1);
                            notifyDataSetChanged();
                        }
                    });

                    convertView = new SwipeItemLayout(view1, view2, null, null);
                    convertView.setTag(vh);
                    break;
            }
        }else {
            switch (type){
                case TYPE_1:
                    break;
                case TYPE_2:
                    vh = (ViewHolder) convertView.getTag();
                    break;
            }
        }

        switch (type){
            case TYPE_1:
                break;
            case TYPE_2:
                vh.cvs_icon.setImageResource(als.get(position-1).getConversationIconId());
                vh.cvs_name.setText(als.get(position-1).getCvsName());
                vh.cvs_content.setText(als.get(position-1).getCvsContent());
                vh.cvs_date.setText(als.get(position-1).getCvsDate());
                break;
        }


        return convertView;
    }

    class ViewHolder {
        public ImageView cvs_icon;
        public TextView cvs_name;
        public TextView cvs_content;
        public TextView cvs_date;
        public TextView cvs_top;
        public TextView cvs_delete;
    }

}
